﻿using Admin.CommonLib.Models.Permission;
using FluentValidation;

namespace Admin.CommonLib.Validators
{
    public class PermissionEditValidator : AbstractValidator<PermissionEditRequestDto>
    {

        public PermissionEditValidator()
        {
            RuleFor(i => i.Code).NotEmpty()
                .WithMessage("权限编码为必填项");

            RuleFor(i => i.Name).NotEmpty()
                .WithMessage("权限名称为必填项");

            RuleFor(i => i.PermissionType).NotEmpty()
                .WithMessage("权限类别为必填项");
        }

        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<PermissionEditRequestDto>.CreateWithOptions((PermissionEditRequestDto)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}